<template lang="pug">
  .CreateWagesModal
    Modal(
      v-model="isShowCreateWagesModal"
      :title="isCreate ? '新增待遇参数' : '修改待遇参数'"
      class-name="vertical-center-modal"
      width="480"
    )
      Form.ivu-row(ref="formCreate" :model="formCreate" :rules="rules" :label-width="100")
        FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
          template(v-if="item.type === 'input'")
            Input(v-model="formCreate[item.model]" :placeholder="item.placeholder" clearable)
          template(v-if="item.type === 'select'")
            Select(v-model="formCreate[item.model]" :placeholder="item.placeholder" clearable :disabled="isCreate ? false : true")
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.label}}
          template(v-if="item.type === 'switch'")
            i-switch(v-model="formCreate[item.model]")
          template(v-if="item.type === 'radio'")
            RadioGroup.top-3-fu.relative(v-model="formCreate[item.model]")
              Radio.display-inline-block(v-for="(c_item, index) of arrListDown[item.options]" :key="index" :label="c_item.value") {{c_item.label}}
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSave()" :loading="!isCanSubmit") 保存
</template>

<script>
import { Common } from '@/utils/common.js'
import { TreatmentApi } from '@/api/Treatment.api.js'
export default {
  name: 'CreateWagesModal',

  // props: ['arrListDown'],

  watch: {
  },

  computed: {
  },

  data () {
    return {
      isShowCreateWagesModal: false,
      isCreate: true,
      formCreate: {
        id: '',
        rowVersion: '',
        parameterType: '',
        parameterName: '',
        whetherDistinguishCitie: '1',
        parameterStatus: -1
      },
      formItem: [
        { type: 'select', label: '业务', placeholder: '请选择业务', prop: 'parameterType', model: 'parameterType', style: 'ivu-col-span-24', options: 'parametertype' },
        { type: 'input', label: '业务类型', placeholder: '请输入业务类型', prop: 'parameterName', model: 'parameterName', style: 'ivu-col-span-24' },
        { type: 'radio', label: '区分城市', placeholder: '', prop: 'whetherDistinguishCitie', model: 'whetherDistinguishCitie', style: 'ivu-col-span-24', options: 'defaultRadioArr' },
        // { type: 'radio', label: '状态', placeholder: '', prop: 'parameterStatus', model: 'parameterStatus', style: 'ivu-col-span-24', options: 'parameterstatus' },
      ],
      rules: {
        parameterType: [
          { required: true, message: '请选择业务', trigger: 'change' }
        ],
        parameterName: [
          { required: true, message: '请输入业务类型', trigger: 'change' }
        ],
        whetherDistinguishCitie: [
          { required: true, message: '请选择是否区分城市', trigger: 'change' }
        ]
      },
      arrListDown: {
        defaultRadioArr: null,
        parametertype: null,
        parameterstatus: null
      },
      isCanSubmit: true
    }
  },

  mounted () {
  },

  methods: {
    cancel () {
      this.isShowCreateWagesModal = false
    },

    show () {
      // this.formCreate.parameterId = id
      this.isShowCreateWagesModal = true
      this.$refs.formCreate.resetFields()
      this.isCanSubmit = true
    },

    // 根据ID获取待遇参数
    async getModel (id) {
      let res = await TreatmentApi.getTreatmentParameter({ParameterId: id})
      console.log('根据ID获取待遇参数: ', res)
      if (res.status === 200 && res.data.success) {
        var data = res.data.data.data
        Object.keys(this.formCreate).forEach(key => {
          this.formCreate[key] = data[key] + '' && data[key] + '' !== 'null' ? data[key] + '' : ''
        })
      } else {
      }
    },

    // 保存
    async handleSave () {
      this.isCanSubmit = false
      console.log('this.formCreate', this.formCreate)
      this.$refs.formCreate.validate((valid) => {
        if (valid) {
          TreatmentApi.saveTreatmentParameter(this.formCreate).then(res => {
            console.log('保存待遇参数: ', res)
            if (res.status === 200 && res.data.success) {
              this.$Message.success(res.data.message)
              this.cancel()
              this.$emit('query')
            } else {
              this.isCanSubmit = true
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        } else {
          this.isCanSubmit = true
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
